﻿using System;
using System.Globalization;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace XnaXaml
{
    public static class ColorExtensions
    {
        public static Color FromHex(this Color color, string hex)
        {
            if (hex.Length < 9) return Color.White;
            var r = Int32.Parse(hex.Substring(1, 2), NumberStyles.HexNumber);
            var g = Int32.Parse(hex.Substring(3, 2), NumberStyles.HexNumber);
            var b = Int32.Parse(hex.Substring(5, 2), NumberStyles.HexNumber);
            var a = Int32.Parse(hex.Substring(7, 2), NumberStyles.HexNumber);

            return new Color(r, g, b, a);
        }

        public static Color FromName(this Color color, string name)
        {
            var props = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);

            for (var i = 0; i < props.Length; i++)
            {
                var prop = props[i];
                if (prop.Name == name)
                {
                    return (Color)prop.GetValue(new Color(), null);
                }
            }

            return Color.White;
        }
    }
}
